# Include template pipeline jobs
include: '.gitlab-ci.yml'

stages:
  - build
  - test
  - deploy
  - project-template-test

# Use the GitLab Runner to test all available jobs of the project template .gitlab-ci.yml
runner-commands:
  stage: project-template-test
  image: docker:latest
  services:
    - name: docker:dind
  when: always
  before_script:
    - apk add gitlab-runner git yq
    - git --version
    - gitlab-runner -v
    - docker info
    - ssh-keygen -t rsa -b 4096 -f gitlab_key -C gitlab -q -N ""
  script: |
    for JOB in $(yq 'to_entries | .[] | .key' .gitlab-ci.yml); do
      ignored="stages cache .add_ssh"
      if echo $ignored | grep $JOB; then
          continue
      fi

      gitlab-runner exec docker "${JOB}" --docker-volumes `pwd`:/builds/project-0 --env GIT_STRATEGY="none" --env SSH_PRIVATE_KEY="$SSH_PRIVATE_KEY"
    done

composer-scripts:
  stage: project-template-test
  image: docker:latest
  services:
    - name: docker:dind
  when: always
  before_script:
    - apk add gitlab-runner git yq composer
    - git --version
    - gitlab-runner -v
    - docker info
  script:
    - composer run-script runner:assets
    - composer run-script runner:install
    - composer run-script runner:fixer
    - composer run-script runner:phpstan

# Override settings, just see if expected binaries are available
# Maybe there is a way to test a deployment, but this is not my business.
.deployer_script: &deployer_script
  script:
    - ./vendor/bin/dep --version || (echo -e "\033[31mDeployer command not found or at least failed to run!\033[m" && exit 1)
    - rsync --version || (echo -e "\033[31mRsync command not found or failed to run!\033[m" && exit 1)
  only:

deploy_to_staging:
  <<: *deployer_script

deploy_to_production:
  <<: *deployer_script
